package org.iks.user.enums.handlers;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.iks.user.enums.ApplySourceType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Author kzou
 * Date 2024/5/23
 * Description: Mybatis {@link ApplySourceType} handler
 */
@MappedTypes({ApplySourceType.class})
@MappedJdbcTypes(JdbcType.INTEGER)
public class ApplySourceTypeTypeHandler extends BaseTypeHandler<ApplySourceType> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, ApplySourceType parameter, JdbcType jdbcType) throws SQLException {
        ps.setInt(parameter.getCode(), i);
    }

    @Override
    public ApplySourceType getNullableResult(ResultSet rs, String columnName) throws SQLException {
        int code = rs.getInt(columnName);
        return ApplySourceType.of(code);
    }

    @Override
    public ApplySourceType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        int code = rs.getInt(columnIndex);
        return ApplySourceType.of(code);
    }

    @Override
    public ApplySourceType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        int code = cs.getInt(columnIndex);
        return ApplySourceType.of(code);
    }

}
